<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla controlleradmin library
jimport('joomla.application.component.controlleradmin');
 
/**
 * NGMenu Controller
 */
class NGMenuControllerNGMenus extends JControllerAdmin
{
	/**
	 * Constructor.
	 *
	 * @param	array	$config	An optional associative array of configuration settings.
	 *
	 * @return	NGMenuControllerNGMenus
	 * @see		JController
	 * @since	1.6
	 */
	public function __construct($config = array())
	{
		parent::__construct($config);
	}

	/**
	 * Proxy for getModel.
	 * @since	1.6
	 */
	public function getModel($name = 'NGMenu', $prefix = 'NGMenuModel') 
	{
		$model = parent::getModel($name, $prefix, array('ignore_request' => true));
		return $model;
	}
	
	/**
	 * Proxy for delete.
	 * @since	1.6
	 */
	function delete()
	{
		$cids = JRequest::getVar('cid', array(), '', 'array');
		$ngitem_model = $this->getModel('NGItem', $prefix = 'NGMenuModel');
		$db =& JFactory::getDBO();
		
		foreach ($cids as $cid){
			$ngitems = $ngitem_model->getItem('ngmenu_id', $cid, $getlist=true);
			foreach ($ngitems as $ngitem){
				$ngitem_model->delete($ngitem->id);
			}
			
			$ngmenu = $this->getModel('NGMenu', $prefix = 'NGMenuModel');
			$menu = $ngmenu->getItem($cid);
			
			$query = $db->getQuery(true);
			$query->delete('#__modules');
			$query->where("id='".$menu->module_id."'");
			$db->setQuery($query);
			$db->query();

			$query = $db->getQuery(true);
			$query->delete('#__modules_menu');
			$query->where("moduleid='".$menu->module_id."'");
			$db->setQuery($query);
			$db->query();
		}
		parent::delete();
	}
	
	function publish()
	{
		$this->setRedirect('index.php?option=com_ngmenu&view=ngmenus');
	}
}
